//
//  HttpData.m
//  objcPostdata
//
//  Created by mac on 12-3-2.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "HttpData.h"

#define HttpURL @"http://zst.cjcp.com.cn/cjwklsf_tianjin/view/klsf_jb.php"
//#define HttpURL @"http://zst.cjcp.com.cn/cjwklsf_tianjin/view/klsf_weishu.php?typeId=yiwei&xingId=1"
@implementation HttpData

static HttpData *_httpDataInstance=nil;

+(HttpData*)HttpDataInstance{
    if (!_httpDataInstance) {
        _httpDataInstance=[[HttpData alloc] init];
    }
    return _httpDataInstance;
}

-(void)pushDataByPost:(NSString*)postdata{
    [self pushDataByPost:postdata url:HttpURL];
}

- (void)postDataAnddelegate:(NSString *)url postdata:(NSString *)postdata httpdelegate:(id<HttpPostDataDelegate>)delegate {
    
    httpdelegate=delegate;
    //NSString *urlString = [NSString stringWithFormat:url];
    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    [request setURL:[NSURL URLWithString:url]];
    [request setHTTPMethod:@"POST"];
    
    //set headers
    NSString *contentType = [NSString stringWithFormat:@"text/xml"];
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];
    
    //[request setValue:@"20120512001" forKey:@"startqi"];
    //[request setValue:@"20990101001" forKey:@"endqi"];
    NSString* requeststring=@"&startqi=20120512001&endqi=20990101001";
    
    NSData *myRequestData = [ NSData dataWithBytes: [ requeststring UTF8String ] length: [ requeststring length ] ];
    
    //create the body
    NSMutableData *postBody = [NSMutableData data];
    
    [postBody appendData:[postdata dataUsingEncoding:NSUTF8StringEncoding]];
    
    [postBody appendData:myRequestData];
    
    //post
    [request setHTTPBody:postBody];
    
    //get response
    NSHTTPURLResponse* urlResponse = nil;  
    NSError *error = [[NSError alloc] init];  
    
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];  
    NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    NSLog(@"Response Code: %d result = %@", [urlResponse statusCode],result);
    if ([urlResponse statusCode] >= 200 && [urlResponse statusCode] < 300) {
        if (httpdelegate&&[httpdelegate respondsToSelector:@selector(postDataSuccess:)]) {
            [httpdelegate postDataSuccess:urlResponse];
        }
        
        NSLog(@"Response: %@", result);
    }
    else{
        if (httpdelegate&&[httpdelegate respondsToSelector:@selector(postDataFail:error:)]) {
            [httpdelegate postDataFail:urlResponse error:error];
        }
    }
}

-(void)sentpostData{
//    NSURL *url = [NSURL URLWithString:HttpURL];
//    
//    
//    
//    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
//    //ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithURL:url] autorelease];
//    [request setPostValue:@"startqi"forKey:@"20120512010"];
//    [request setPostValue:@"endqi"forKey:@"20990101001"];
//    //[request setDelegate:self];
//    [request startSynchronous];
//    NSError*error = [request error];
//    if(!error) {
//        NSString*response = [request responseString];
//        NSLog(@"response = %@",response);
//    }
}

-(void)pushDataByPost:(NSString*)postdata url:(NSString*)url{
    [self postDataAnddelegate:url postdata:postdata httpdelegate:nil];
}

-(void)end{
    
}

-(void)dealloc{
    NSLog(@"HttpData deallocing...");
    [super dealloc];
}

@end
